package ctn.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import ctn.contant.UserConstant;
import ctn.convert.CTNUserConvert;
import ctn.dao.inteface.CTNUserDao;
import ctn.hibernate.po.CtnUser;
import ctn.service.dto.CTNUserDTO;
import ctn.service.inteface.UserService;
import ctn.util.SecurityUtil;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private CTNUserDao userDao;
	
	@Override
	public int validateUserName(String username) {
		return userDao.validateUserName(username);
	}
	
	@Override
	public int validateUserEmail(String email) {
		return userDao.validateUserEmail(email);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public CTNUserDTO saveUser(CTNUserDTO ctnUserDTO) {
		CtnUser user = new CtnUser();
		ctnUserDTO.setCreateDate(new Date());
		ctnUserDTO.setStatus(UserConstant.USER_STATUS_ACTIVITY);
		
		CTNUserConvert.convertDTO2Po(ctnUserDTO, user);
		try {
			String encodePwd = new String(SecurityUtil.encode(user.getPassword(), UserConstant.USER_SECURITY_KEY_DEFAULT));
			user.setPassword(encodePwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		CTNUserConvert.convertPo2DTO(userDao.saveUser(user),ctnUserDTO);
		return ctnUserDTO;
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updateUser(CTNUserDTO ctnUserDTO){
		ctnUserDTO.setUpdateDate(new Date());
		CtnUser user = new CtnUser();
		CTNUserConvert.convertDTO2Po(ctnUserDTO, user);
		userDao.updateUser(user);
	}

	@Override
	public CTNUserDTO doLogin(CTNUserDTO ctnUserDTO) {
		CtnUser user = new CtnUser();
		CTNUserConvert.convertDTO2Po(ctnUserDTO, user);
		//加密登录密码后进行比较
		try {
			String encodePwd = new String(SecurityUtil.encode(user.getPassword(), UserConstant.USER_SECURITY_KEY_DEFAULT));
			user.setPassword(encodePwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<CtnUser> users = userDao.getUsers(user);
		if(users.size()==0)
			return null;
		else{
			/*//如果该用户为离线状态，则设置该用户为活动状态
			user = users.get(0);
			if(user.getActivity()==0){
			user.setActivity(1);
			userDao.updateUser(user);
			}*/
			CTNUserConvert.convertPo2DTO(user, ctnUserDTO);
		 	return ctnUserDTO;
		}
	}
	
	@Override
	public CTNUserDTO forgetPass(CTNUserDTO ctnUserDTO) {
		CtnUser user = new CtnUser();
		CTNUserConvert.convertDTO2Po(ctnUserDTO, user);
		List<CtnUser> users = userDao.getUsers(user);
		if(users.size()==0)
			return null;
		else{
			CtnUser s_user = users.get(0);
			CTNUserConvert.convertPo2DTO(s_user, ctnUserDTO);
		 	return ctnUserDTO;
		}
	}
	
	@Override
	public CTNUserDTO resetPass(CTNUserDTO ctnUserDTO) {
		String newPass = ctnUserDTO.getPassword();
		CtnUser user = new CtnUser();
		CTNUserConvert.convertDTO2Po(ctnUserDTO, user);
		user.setPassword(null);
		List<CtnUser> users = userDao.getUsers(user);
		if(users.size()==0)
			return null;
		else{
			CtnUser s_user = users.get(0);
			CTNUserConvert.convertPo2DTO(s_user, ctnUserDTO);
			//��������
			String encodePwd = "";
			try {
				encodePwd = new String(SecurityUtil.encode(newPass, UserConstant.USER_SECURITY_KEY_DEFAULT));
			} catch (Exception e) {
				e.printStackTrace();
			}
			//���������ý�pojo���������
			s_user.setPassword(encodePwd);
			s_user =  userDao.updateUser(s_user);
			CTNUserConvert.convertPo2DTO(s_user, ctnUserDTO);
		 	return ctnUserDTO;
		}
	}
	
	public boolean validatePwd(String pwd,Long id){
		boolean flag = false;
		try {
			String encodePwd = new String(SecurityUtil.encode(pwd, UserConstant.USER_SECURITY_KEY_DEFAULT));
			flag = userDao.validatePwd(encodePwd, id);
			return flag;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public void resetEmailOrName(CTNUserDTO ctnUserDTO,String property){
		if("email".equals(property)){
			userDao.resetEmailOrName(null,ctnUserDTO.getEmail(), ctnUserDTO.getID());
		}else if("name".equals(property)){
			userDao.resetEmailOrName(ctnUserDTO.getUserName(),null, ctnUserDTO.getID());
		}
	}
	
	public CTNUserDTO getCTNUserDTOById(Long id){
		CtnUser po = userDao.getUserByID(id);
		CTNUserDTO ctnUserDTO = new CTNUserDTO();
		CTNUserConvert.convertPo2DTO(po, ctnUserDTO);
		return ctnUserDTO;
	}

	@Override
	public CtnUser getCtnUser(CTNUserDTO uv1) {
		// TODO Auto-generated method stub
		return userDao.getUserByName(uv1.getUserName());
	}

	@Override
	public CtnUser getCtnUserByID(String userID) {
		// TODO Auto-generated method stub
		return userDao.getUserByID(Long.parseLong(userID));
	}
}
